package com.thinta.wind.network

import com.thinta.wind.JetPackDMApp
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/*********************************************************
 * 版权信息：2021/5/11 15:11北京振中电子技术有限公司版权所有<p>
 * 创建作者：Wind<p>
 * 创建日期：2021/5/11 15:11<p>
 * *******************************************************<p>
 * 更新历史：    作者      日期      更新摘要<p>
 * <p>
 * *******************************************************<p>
 * 类功能说明：<p>
 */
object RetrofitManager {

    val apiService: ApiService by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){
        Retrofit.Builder()
            .baseUrl(JetPackDMApp.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(genericOkClient())
            .build().create(ApiService::class.java)
    }

    val apiServiceAlpha: ApiService by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){
        Retrofit.Builder()
            .baseUrl("http://baobab.kaiyanapp.com")
            .addConverterFactory(GsonConverterFactory.create())
            .client(genericOkClient())
            .build().create(ApiService::class.java)
    }


    private fun genericOkClient(): OkHttpClient {
        val httpLoggingInterceptor = HttpLoggingInterceptor(
            object : HttpLoggingInterceptor.Logger {
                override fun log(message: String) {

                }
            })
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        return OkHttpClient.Builder()
            .connectTimeout(5000L, TimeUnit.MILLISECONDS)
            .readTimeout(10_000, TimeUnit.MILLISECONDS)
            .writeTimeout(30_000, TimeUnit.MILLISECONDS)
            .addInterceptor(httpLoggingInterceptor)
            .build()
    }

}